package com.pets.web.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.pets.common.utils.StringUtils;
import com.pets.web.dto.SoldDto;
import com.pets.web.entity.PetsComment;
import com.pets.web.entity.PetsGoods;
import com.pets.web.entity.PetsOrders;
import com.pets.web.entity.PetsShop;
import com.pets.web.enums.GoodEnums;
import com.pets.web.mapper.PetsGoodsMapper;
import com.pets.web.service.IPetOrdersService;
import com.pets.web.service.IPetsCommentService;
import com.pets.web.service.IPetsGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2024-05-25
 */
@Service
public class PetsGoodsServiceImpl extends ServiceImpl<PetsGoodsMapper, PetsGoods> implements IPetsGoodsService {

    @Autowired
    private IPetOrdersService petOrdersService;

    @Override
    public List<PetsGoods> list(PetsGoods petsGoods) {
        MPJLambdaWrapper<PetsGoods> wrapper = JoinWrappers.lambda(PetsGoods.class)
                .selectAll(PetsGoods.class)
                .selectAs(PetsShop::getName, PetsGoods::getShopName)
                .selectAs(PetsShop::getAvatar, PetsGoods::getShopAvatar)
                .leftJoin(PetsShop.class, PetsShop::getId, PetsGoods::getShopId)
                .like(StringUtils.isNotEmpty(petsGoods.getName()), PetsGoods::getName, petsGoods.getName())
                .eq(Objects.nonNull(petsGoods.getStatus()), PetsGoods::getStatus, petsGoods.getStatus())
                .eq(Objects.nonNull(petsGoods.getSysUserId()), PetsGoods::getSysUserId, petsGoods.getSysUserId());
        return baseMapper.selectJoinList(PetsGoods.class, wrapper);
    }

    @Override
    public boolean offline(Integer goodsId) {
        return lambdaUpdate().set(PetsGoods::getStatus, GoodEnums.OFFLINE).eq(PetsGoods::getId, goodsId).update();
    }

    @Override
    public boolean online(Integer goodsId) {
        return lambdaUpdate().set(PetsGoods::getStatus, GoodEnums.ONLINE).eq(PetsGoods::getId, goodsId).update();
    }

    @Override
    public PetsGoods getGoodsById(Integer goodsId) {
        MPJLambdaWrapper<PetsGoods> wrapper = JoinWrappers.lambda(PetsGoods.class)
                .selectAll(PetsGoods.class)
                .selectAs(PetsShop::getName, PetsGoods::getShopName)
                .selectAs(PetsShop::getAvatar, PetsGoods::getShopAvatar)
                .leftJoin(PetsShop.class, PetsShop::getId, PetsGoods::getShopId)
                .eq(PetsGoods::getId, goodsId)
                .eq(PetsGoods::getStatus, GoodEnums.ONLINE);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public SoldDto getSoldInfo(Integer goodsId) {
        SoldDto soldDto = new SoldDto();
        Long soldNum = petOrdersService.countByGoodsId(goodsId);
        Long soldWeight = petOrdersService.sumWeightByGoodsId(goodsId);
        soldDto.setSoldNum(soldNum);
        soldDto.setPayNum(soldNum);
        List<PetsOrders> petsOrdersList = petOrdersService.getOrderList(goodsId);
        List<String> avatars = petsOrdersList.stream().sorted(Comparator.comparing(PetsOrders::getCreateTime)).map(PetsOrders::getAvatar).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        soldDto.setAvatars(avatars);
        PetsGoods goods = baseMapper.selectById(goodsId);
        soldDto.setTarget(goods.getTarget());
        soldDto.setPayWeight(soldWeight);
        soldDto.setRate(NumberUtil.div(soldWeight, goods.getTarget(), 4).multiply(new BigDecimal(100)));
        return soldDto;
    }


    @Override
    public int add(PetsGoods petsGoods) {
        return baseMapper.insert(petsGoods);
    }

    @Override
    public int deleteById(Integer goodsId) {
        return baseMapper.deleteById(goodsId);
    }
}
